package com.endava.nio_io;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Nio_SimpleReading {
	public static void main(String[] args) throws IOException {
		// create the file 'myfile'
		File f = new File("myfile.txt");
		if (f.exists() == false) {
			f.createNewFile();
		} else {
			System.out.println(f);
		}

		FileInputStream fin = new FileInputStream("myfile.txt");
		FileChannel fc = fin.getChannel();
		ByteBuffer buffer = ByteBuffer.allocate(1024);// create buffer

		fc.read(buffer);// read from channel into buffer
		// System.out.println(fc);

		// reading the buffer
		buffer.flip();
		int limit = buffer.limit();// how many chars in buffer
		for (int i = 0; i < limit; i++) {
			System.out.print((char) buffer.get());
		}
		fin.close();
		fc.close();

	}
}
